package top.lbqaq.servlet;

import top.lbqaq.pojo.Material;
import top.lbqaq.pojo.Product;
import top.lbqaq.service.MaterialService;
import top.lbqaq.service.ProductService;
import top.lbqaq.util.pageBean;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author Gor
 */
public class ProductServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String method = req.getParameter("method");
    if(method==null)
    {method= (String) req.getParameter("method1");}
    String name = req.getParameter("name");
    String[] checks =req.getParameterValues("check");
    String type = req.getParameter("type");
    String id = req.getParameter("id");

    String delete =req.getParameter("delete");
    String insert =req.getParameter("insert");
    String p= req.getParameter("pageNum");
        int pageNum;
    if(p==null)
    { pageNum=1;}
    else {
        pageNum= Integer.parseInt(p);
    }
    if("selectAll".equals(method))
    { ProductService productService = new ProductService();
        int pagesize=5;
        pageBean<Product> productList = productService.selectAllProduct(pageNum, pagesize);
        req.setAttribute("productList", productList);
        req.setAttribute("method1","selectAll");
        req.getRequestDispatcher("/jsp/productselect.jsp").forward(req, resp);}
    if("selectByCategory1".equals(method))
    { ProductService productService = new ProductService();
        int pagesize=5;
        pageBean<Product> productList = productService.selectByCategory1(name,pageNum, pagesize);
        req.setAttribute("productList", productList);
        req.setAttribute("method1","selectByCategory1");
        req.getRequestDispatcher("/jsp/productselect.jsp").forward(req, resp);}
    if("selectByCategory2".equals(method))
        { ProductService productService = new ProductService();

            List<Product> productList = productService.selectByCategory2(name);
            req.setAttribute("productList", productList);
            req.getRequestDispatcher("/jsp/productselect.jsp").forward(req, resp);}
        if("selectByName".equals(method))
        { ProductService productService = new ProductService();
            List<Product> productList = productService.selectByName(name);
            req.setAttribute("productList", productList);
            req.getRequestDispatcher("/jsp/productselect.jsp").forward(req, resp);}
        if(checks!=null&&delete!=null)
        {   for (int i = 0;i < checks.length; i++) {
            ProductService productService = new ProductService();
            int n=0;
           n= productService.delectByNo(checks[i]);
           if(n!=0)
           {req.setAttribute("msg", "删除成功");
            req.getRequestDispatcher("/jsp/productselect.jsp").forward(req, resp);}
           else {req.setAttribute("error", "删除失败");
               req.getRequestDispatcher("/error.jsp").forward(req, resp);
           }
        }
        }
        if (insert!=null){
            req.setAttribute("msg", "插入");
            req.getRequestDispatcher("/jsp/px.jsp").forward(req, resp);
        }
        if ("xiu".equalsIgnoreCase(type)) {
            xiugai01(req, resp);
        }

        if("xiu1".equals(type)&&(!"".equals(id))){
            System.out.println(9);
            update(req,resp);
        }
        if("xiu1".equals(type)&&"".equals(id)){
            System.out.println(3);
            insertp(req,resp);
        }

    }
     public void xiugai01(HttpServletRequest request,
             HttpServletResponse response){
         int id = Integer.parseInt(request.getParameter("id"));
         ProductService productService = new ProductService();
       Product product =productService.selectById(id);
         request.setAttribute("product", product);
         try { request.setAttribute("msg", "更新");
             request.getRequestDispatcher("/jsp/px.jsp").forward(
                     request, response);
         } catch (ServletException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         } catch (IOException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }
     }
    public void update(HttpServletRequest req,
                         HttpServletResponse resp) throws ServletException, IOException {
        ProductService productService = new ProductService();
        int id = Integer.parseInt(req.getParameter("id"));
        Product product =new Product();
        product.setId(id);
        product.setProductNo(req.getParameter("No"));
        product.setProductRemark(req.getParameter("remark"));
        product.setProductPicture(req.getParameter("picture"));
        product.setProductUnit(req.getParameter("unit"));
        product.setProductName(req.getParameter("name"));
        product.setProductCategory1(req.getParameter("Category1"));
        product.setProductCategory2(req.getParameter("Category2"));
        int n=productService.updateById(product);
        if(n==0)
        {req.setAttribute("error", "数据更新失败");
            req.getRequestDispatcher("/error.jsp").forward(req, resp);
        } else {
            req.setAttribute("msg", "更新成功");
            req.getRequestDispatcher("/jsp/productselect.jsp").forward(
                    req, resp);
        }

    }
    public void insertp(HttpServletRequest req,
                       HttpServletResponse resp) throws ServletException, IOException {
        ProductService productService = new ProductService();
       // int id = Integer.parseInt(req.getParameter("id"));
        Product product =new Product();
       // product.setId(id);
        product.setProductNo(req.getParameter("No"));
        product.setProductRemark(req.getParameter("remark"));
        product.setProductPicture(req.getParameter("picture"));
        product.setProductUnit(req.getParameter("unit"));
        product.setProductName(req.getParameter("name"));
        product.setProductCategory1(req.getParameter("Category1"));
        product.setProductCategory2(req.getParameter("Category2"));

        int n=productService.insertproduct(product);
        if(n==0)
        {req.setAttribute("error", "插入失败");
            req.getRequestDispatcher("/error.jsp").forward(req, resp);
        } else {
            req.setAttribute("msg", "插入成功");
            req.getRequestDispatcher("/jsp/productselect.jsp").forward(
                    req, resp);
        }

    }
//        try {
//            request.getRequestDispatcher("/jsp/px.jsp").forward(
//                    request, response);
//        } catch (ServletException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        } catch (IOException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
